package game.games.gol;

import game.AbstractRunnable.Status;
import game.console.IConsoleCommand;

import java.util.ArrayList;
import java.util.List;

public class GameOfLifeCommands
{
    public static List<IConsoleCommand> getCommands()
    {
        final GameOfLife gol = GameOfLife.getInstance();
        
        List<IConsoleCommand> commands = new ArrayList<IConsoleCommand>();
        commands.add(new IConsoleCommand()
        {
            @Override
            public String getCommand()
            {
                return "pause";
            }
            
            @Override
            public String[] getSynonym()
            {
                return new String[] {};
            }
            
            @Override
            public String getInfo()
            {
                return "pause - pause game - 'pause'";
            }
            
            @Override
            public String[] fireCommand(String command)
            {
                gol.setStatus(Status.PAUSE);
                return new String[] { "Pause = " + gol.isPaused() };
            }
        });
        commands.add(new IConsoleCommand() {
            
            @Override
            public String getCommand()
            {
                return "rules";
            }
            
            @Override
            public String[] getSynonym()
            {
                return new String[] {};
            }
            
            @Override
            public String getInfo()
            {
                return "rules - list some example rules";
            }
            
            @Override
            public String[] fireCommand(String command)
            {
                List<String> rules = new ArrayList<String>();
                for (GameOfLifeRule rule : GameOfLifeRule.getRules())
                {
                    rules.add(rule.getRule());
                }
                return rules.toArray(new String[rules.size()]);
            }
        });
        commands.add(new IConsoleCommand()
        {
            @Override
            public String getCommand()
            {
                return "rule";
            }
            
            @Override
            public String[] getSynonym()
            {
                return new String[] {};
            }
            
            @Override
            public String getInfo()
            {
                return "rule  - change gol rule, param is gol-rule-syntax SURVIVE/BIRTH - 'rule 23/3'";
            }
            
            @Override
            public String[] fireCommand(String command)
            {
                String rule = command.substring("rule ".length());
                gol.setRule(rule);
                return new String[] { "NEW RULE: '" + rule + "'" };
            }
        });
        commands.add(new IConsoleCommand()
        {
            @Override
            public String getCommand()
            {
                return "irule";
            }
            
            @Override
            public String[] getSynonym()
            {
                return new String[] {};
            }
            
            @Override
            public String getInfo()
            {
                return "rule  - change gol rule, param is gol-rule-syntax SURVIVE/BIRTH - 'rule 23/3'";
            }
            
            @Override
            public String[] fireCommand(String command)
            {
                String rule = command.substring("irule ".length());
                GameOfLifeRule golRule = new GameOfLifeRule(rule);
                golRule.invert();
                gol.setRule(golRule);
                return new String[] { "NEW RULE: '" + rule + "'" };
            }
        });
        commands.add(new IConsoleCommand()
        {
            @Override
            public String getCommand()
            {
                return "color";
            }
            
            @Override
            public String[] getSynonym()
            {
                return new String[] {};
            }
            
            @Override
            public String getInfo()
            {
                return "color - change color, available colors: 0 = (green, red, black), 1 = (black, white)";
            }
            
            @Override
            public String[] fireCommand(String command)
            {
                String color = command.substring("color ".length());
                gol.setColorSet(ColorSet.getById(color));
                return new String[] { "NEW COLOR: '" + color + "'" };
            }
        });
        commands.add(new IConsoleCommand()
        {
            @Override
            public String getCommand()
            {
                return "lc%";
            }
            
            @Override
            public String[] getSynonym()
            {
                return new String[] {};
            }
            
            @Override
            public String getInfo()
            {
                return "lc%   - change living cells % of total cells on next restart, WARNING performance decreases extremely when % is greater than 50";
            }
            
            @Override
            public String[] fireCommand(String command)
            {
                int prozent = Integer.parseInt(command.substring("lc% ".length()));
                gol.setProzentLivingCells(prozent);
                return new String[] { "LIVING CELLS ON RESTART: " + prozent + " %" };
            }
        });
        commands.add(new IConsoleCommand()
        {
            @Override
            public String getCommand()
            {
                return "lc";
            }
            
            @Override
            public String[] getSynonym()
            {
                return new String[] {};
            }
            
            @Override
            public String getInfo()
            {
                return "lc   - change living total cells on next restart";
            }
            
            @Override
            public String[] fireCommand(String command)
            {
                int living = Integer.parseInt(command.substring("lc ".length()));
                gol.setLivingCells(living);
                return new String[] { "LIVING CELLS ON RESTART: " + living };
            }
        });
        return commands;
    }
}
